package taro.todolist.guipath;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import taro.todolist.utils.UserUtil;

@Controller
public class TemplateController {

    @Autowired
    UserUtil userUtil;

    @GetMapping("")
    @ResponseBody
    public String root_page(){
        System.out.println("*****NOW AT ROOT*****");
        return "*****NOW AT ROOT*****";
    }

    @GetMapping("/login_page")
    public String login_page(Model model){
        System.out.println("*****NOW AT LOGIN_PAGE*****");
        model.addAttribute("msg", "now at login_page");
        return "login_page";
    }

    @PostMapping("/login")
    public String login(HttpServletRequest request, String username, String password, Model model){
        if ( "root".equals(username) && "12345".equals(password) ){
            userUtil.login(request.getSession(), "admin");
            return "redirect:/catalog_page";
        } else {
            model.addAttribute("msg", "Invalid username or password");
            return "login";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
        userUtil.logout(request.getSession());
        return "redirect:/login";
    }

    @GetMapping("/catalog_page")
    public String catalog_page(){
        return "catalog_page";
    }


}
